﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Xml.Linq;

using ProjectBase.Core;
using ProjectBase.Core.Dao;

namespace $safeprojectname$.Xml
{
    public static class XmlSession
    {
        #region members
        static IXmlRequestSession _storage;
        #endregion members

        #region Initialize and Dispose
        public static void Initialize(IXmlRequestSession storage)
        {
            _storage = storage;
        }

        public static void Dispose()
        {
            if (_storage.IsNull())
            {
                return;
            }
            _storage.XDocuments = null;
            _storage.IsTransaction = false;
        }
        #endregion Initialize and Dispose

        #region Current
        public static IDictionary<string, XDocument> Current
        {
            get
            {
                if (!Global.Settings.EnableXmlDao)
                {
                    return null;
                }
                if (_storage.XDocuments.IsNull())
                {
                    _storage.XDocuments = new Dictionary<string, XDocument>();
                }
                return _storage.XDocuments;
            }
        }
        #endregion Current

        #region transaction
        /// <summary>
        ///     Provides the access to currently existing transaction property 'IsActive'
        /// </summary>
        public static bool TransactionIsActive
        {
            // Wraper: No need to reference underlying stuff on upper layer
            get
            {
                if (Current.IsNull())
                {
                    return false;
                }
                return _storage.IsTransaction;
            }
        }

        /// <summary>
        ///     Provides the access to currently existing ISession's "BeginTransaction()";
        /// </summary>
        public static void TransactionBegin()
        {
            // Wraper: No need to reference underlying stuff on upper layer            
            _storage.IsTransaction = true;
        }

        /// <summary>
        ///     Provides the access to currently existing ISession's "Transaction.Commit()";
        /// </summary>
        public static void TransactionCommit()
        {
            // Wraper: No need to reference underlying stuff on upper layer
            if (_storage.XDocuments.IsNull())
            {
                return;
            }
            _storage.IsTransaction = false;
            foreach (var pair in Current)
            {
                pair.Value.Save(pair.Key);
            }
        }

        /// <summary>
        ///     Provides the access to currently existing ISession's "Transaction.Rollback()";
        /// </summary>
        public static void TransactionRollback()
        {
            // Wraper: No need to reference underlying stuff on upper layer
            if (_storage.XDocuments.IsNull())
            {
                return;
            }
            foreach (var pair in _storage.XDocuments)
            {
                var f = new FileInfo(pair.Key);
                if (f.Exists)
                {
                    f.LastWriteTime = DateTime.Now;
                }
            }
            _storage.IsTransaction = false;
            Clear();
        }
        #endregion transaction

        public static void Clear()
        {
            // Wraper: No need to reference underlying stuff on upper layer
            if (_storage.XDocuments.IsNull())
            {
                return;
            }
            if (_storage.XDocuments.Is())
            {
                _storage.XDocuments = new Dictionary<string, XDocument>();
            }
            _storage.IsTransaction = false;
        }
    }
}